using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	float maxSpeed  {
		get {
			return GameManager.Instance.maxSpeed;
		}
	}

	public LayerMask movementLayerMask;

	// Use this for initialization
	void Start () {
	}
	
	void Update () {

		Vector2 vel = new Vector2(
				Input.GetAxis("Horizontal") * maxSpeed,
				Input.GetAxis("Vertical") * maxSpeed
			);

		if(vel.magnitude > maxSpeed) 
			vel = vel.normalized * maxSpeed;

		/*RaycastHit2D hit = Physics2D.Linecast(transform.position, (Vector2)transform.position + vel*Time.deltaTime, movementLayerMask );
		if(hit.collider != null) {
			vel = Vector2.zero;
		}*/

		rigidbody2D.velocity = vel;

		Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

		transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mousePos.y - this.transform.position.y, mousePos.x - this.transform.position.x) * Mathf.Rad2Deg - 90f);

		if(Input.GetButton("Fire1")) {
			GetComponent<ShootsBullets>().Fire();
		}

		SpriteRenderer sr = GetComponent<SpriteRenderer>();
		if( GetComponent<Health>().IsBurning() ) {
			sr.color = Color.red;
		}
		else {
			sr.color=Color.white;
		}
	}
}
